package server;

import java.util.ArrayList;
import java.util.TimerTask;

import common.IAuctionServer;
import common.Item;

public class AuctionTimerTask extends TimerTask {

	private Item itemToRemove;
	private ArrayList<Item> items;
	private AuctionServer server;
	private int auctionTime;
	
	public AuctionTimerTask(Item itemToRemove, ArrayList<Item> items, AuctionServer server) {
		this.itemToRemove = itemToRemove;
		this.items = items;
		this.server = server;
		this.auctionTime = this.itemToRemove.getAuctionTime();
	}
	
	@Override
	public void run() {
		auctionTime--;
		if(auctionTime == 60) {
			server.notifyListeners(itemToRemove);
		}
		if(auctionTime == 0) {
			items.remove(itemToRemove);
			itemToRemove.setAuctionTime(0);
			server.notifyListeners(itemToRemove);
			System.out.println(String.format("Auction of %s item has finished!", itemToRemove.getName()));
			this.cancel();
		}
	}

}
